﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BoardSquareView.xaml.cs" company="">
//   
// </copyright>
//  <summary>
//   Interaction logic for BoardSquare.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Shatranj
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;

    /// <summary>
    ///     Interaction logic for BoardSquare.xaml
    /// </summary>
    public partial class BoardSquareView : RadioButton
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BoardSquareView"/> class.
        /// </summary>
        public BoardSquareView()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>
        /// The command binding_ executed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
        }

        #endregion

        // protected override void OnClick()
        // {
        // base.OnClick();
        // var context = this.DataContext as BoardSquare;
        // //context.Select(true, MoveOptions.AllApplicable);
        // }
    }

    /// <summary>
    /// The board square view behaviors.
    /// </summary>
    public static class BoardSquareViewBehaviors
    {
        #region Static Fields

        /// <summary>
        /// The is selected property.
        /// </summary>
        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached(
            "IsSelected", 
            typeof(bool), 
            typeof(BoardSquareView), 
            new UIPropertyMetadata(false, OnIsSelectedChanged));

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get is selected property.
        /// </summary>
        /// <param name="square">
        /// The square.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool GetIsSelectedProperty(BoardSquareView square)
        {
            return (bool)square.GetValue(IsSelectedProperty);
        }

        /// <summary>
        /// The set is selected property.
        /// </summary>
        /// <param name="square">
        /// The square.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        public static void SetIsSelectedProperty(BoardSquareView square, bool value)
        {
            square.SetValue(IsSelectedProperty, value);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on is selected changed.
        /// </summary>
        /// <param name="depObj">
        /// The dep obj.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private static void OnIsSelectedChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            var item = depObj as BoardSquareView;
            if (item == null)
            {
                return;
            }

            if (e.NewValue is bool == false)
            {
                return;
            }

            if ((bool)e.NewValue)
            {
                item.PreviewMouseDown += OnIsSelectedChanged;
            }
            else
            {
                item.PreviewMouseDown -= OnIsSelectedChanged;
            }
        }

        /// <summary>
        /// The on is selected changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private static void OnIsSelectedChanged(object sender, RoutedEventArgs e)
        {
            // Only react to the Selected event raised by the TreeViewItem
            // whose IsSelected property was modified. Ignore all ancestors
            // who are merely reporting that a descendant's Selected fired.
            if (!object.ReferenceEquals(sender, e.OriginalSource))
            {
                return;
            }

            var item = e.OriginalSource as BoardSquareView;
            if (item != null)
            {
                item.Background = new SolidColorBrush(Color.FromRgb(34, 39, 111));
            }
        }

        #endregion
    }
}